#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include <QSqlDatabase>
#include <QSqlError>
#include <QDebug>
#include <QCursor>

#include <QSettings>
#include "Server.h"



#include "Game.h"
#include "Adapter.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    app.setOrganizationName("LeiTian");
    app.setOrganizationDomain("leitian.com");
    app.setApplicationName("Application");

    qDebug() << app.applicationDirPath();
    QSettings config(app.applicationDirPath() + "/config.ini", QSettings::IniFormat);

    if(false == config.value("NeedCursor", true).toBool()){
            app.setOverrideCursor(QCursor(Qt::BlankCursor));
    }

    QString host  = config.value("Host", "47.99.240.14").toString();
    qDebug() << "Server host is" << host;


    Server * server  = Server::getInstance();
    server->setHost(host);

    // 初始化数据库
    QSqlDatabase db  = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(app.applicationDirPath() + "/questions.db");
    if(!db.open()){
        qDebug() << "数据库打开失败:" << db.lastError().text();
        exit(-1);
    }

    Adapter adapter;

    adapter.init(config.value("Port", "COM3").toString());

    Game    game;

    if( !game.loadRoundFromDatabase() ){
        qDebug() << "载入题库失败!";
        exit(-1);
    }


    QObject::connect(&adapter, SIGNAL(braceletReady(quint32, QByteArray)), &game, SLOT(validatePlayer(quint32, QByteArray)));
    QObject::connect(&adapter, SIGNAL(competationCommit(quint32)), &game, SLOT(validateCompetitor(quint32)));
    QObject::connect(&adapter, SIGNAL(selectionCommit(quint32,int)), &game, SLOT(validateSelection(quint32,int)));

    QObject::connect(&game, SIGNAL(askToReset()), &adapter, SLOT(reset()));
    QObject::connect(&game, SIGNAL(askToReadBracelet(quint32)), &adapter, SLOT(readBraceletSn(quint32)));
    QObject::connect(&game, SIGNAL(askToCompete()), &adapter, SLOT(startToCompete()));

    QObject::connect(server, SIGNAL(requestStudentIdSuccessed(const QString ,  qint64)), &game, SLOT(onStudentRequestSuccess(const QString , const qint64)));
    QObject::connect(server, SIGNAL(requestStudentIdFailed(int, const QString )), &game, SLOT(onStudentRequestFail(int,const QString)));


    if(!game.loadSetting()){
        qDebug() << "载入游戏设置失败!将使用默认设置.";
    }


    qmlRegisterType<Game>("Game", 1, 0, "Game");
    qmlRegisterType<GameSetting>("GameSetting", 1, 0, "GameSetting");


    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);

    //engine.rootContext()->setContextProperty("Setting", &config);
    engine.rootContext()->setContextProperty("ApplicationPath", app.applicationDirPath());
    engine.rootContext()->setContextProperty("Game", &game);

    engine.load(url);

    return app.exec();
}
